package com.eric.husky.service;

import com.eric.husky.common.utils.UpdateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.eric.husky.dao.AdDao;
import com.eric.husky.bean.Ad;
import java.util.List;

@Service
public class AdService {
    @Autowired
	private AdDao adDao;

	public Page<Ad> list(Ad ad, Pageable page) {
		Example<Ad> example = Example.of(ad);
		return adDao.findAll(example, page);
	}

	public Ad get(Integer adid) {
		return adDao.findById(adid).get();
	}

    public List<Ad> findList() {
		return adDao.findAll();
	}

	@Transactional
	public Ad save(Ad ad) {
	    if(ad.getAdid()!=null){
	        Ad adOld = adDao.findById(ad.getAdid()).orElse(null);
	        if(adOld!=null){
	            UpdateUtil.copyNullProperties(ad,adOld);
                return adDao.save(adOld);
	        }
	    }
	    return adDao.save(ad);
	}

	@Transactional
	public void delete(Integer adid) {
		adDao.deleteById(adid);
	}

}